home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / TouchMe 1.2□ / touchMe 1.2 Folder / touchMe source codes / CW11 PP source / source / Common Lib / UDateTime.cp < prev    next >
Encoding:
Text File  |  1997-04-25  |  4.1 KB  |  160 lines  |  [TEXT/CWIE]

  1. // ==================================================
  2. //    UDateTime.cp
  3. //    Copyright (C) 1994-1997 Mizutori Tetsuya
  4. //    March 9, 1994; July 4, 1996; February 3, 1997.
  5. // ==================================================
  6. //    All documents are pretty-printed in 10-point Geneva font.
  7.  
  8. #include <TextUtils.h>
  9. #include <Script.h>
  10. #include <IntlResources.h>
  11. #include <OSUtils.h>
  12.  
  13. #include <UMemoryMgr.h>
  14. #include <UDebugging.h>
  15.  
  16. #include "UDateTime.h"
  17.  
  18. #define longDateTimeHi( a )    (((LongDateCvt *) &(a))->hl.lHigh)
  19. #define longDateTimeLo( a )    (((LongDateCvt *) &(a))->hl.lLow)
  20.  
  21.  
  22. #define    kWantSeconds            true
  23. #define    k_intl_NumericFormat    0
  24. #define    k_intl_LongDateFormat    1
  25.  
  26.  
  27. // --------------------------------------------------
  28. //        • SecondsToDateTimeString
  29. // --------------------------------------------------
  30.  
  31. void
  32. UDateTime::SecondsToDateTimeString(
  33.     const unsigned long    inSeconds,
  34.     Str255            outDateString,
  35.     Str255            outTimeString )
  36. {
  37.     LongDateRec    theDateTime;
  38.  
  39.     SecondsToDateTime( inSeconds, theDateTime );
  40.  
  41.     DateTimeToString( theDateTime, outDateString, outTimeString );
  42. }
  43.  
  44.  
  45. // --------------------------------------------------
  46. //        • DateTimeStringToSeconds
  47. // --------------------------------------------------
  48.  
  49. void
  50. UDateTime::DateTimeStringToSeconds(
  51.     const Str255    inDateString,
  52.     const Str255    inTimeString,
  53.     unsigned long &    outSeconds )
  54. {
  55.     LongDateRec    theDateTime;
  56.  
  57.     StringToDateTime( inDateString, inTimeString, theDateTime );
  58.  
  59.     DateTimeToSeconds( theDateTime, outSeconds );
  60. }
  61.  
  62.  
  63. // --------------------------------------------------
  64. //        • SecondsToDateTime
  65. // --------------------------------------------------
  66.  
  67. void
  68. UDateTime::SecondsToDateTime(
  69.     const unsigned long    inSeconds,
  70.     LongDateRec &        outDateTime )
  71. {
  72.     LongDateTime    theDateTimeSecs;
  73.  
  74.     longDateTimeHi( theDateTimeSecs ) = 0;
  75.     longDateTimeLo( theDateTimeSecs ) = inSeconds;
  76.  
  77.     ::LongSecondsToDate( &theDateTimeSecs, &outDateTime );
  78. }
  79.  
  80.  
  81. // --------------------------------------------------
  82. //        • DateTimeToSeconds
  83. // --------------------------------------------------
  84.  
  85. void
  86. UDateTime::DateTimeToSeconds(
  87.     const LongDateRec &    inDateTime,
  88.     unsigned long &        outSeconds )
  89. {
  90.     LongDateTime    theDateTimeSecs;
  91.  
  92.     LongDateToSeconds( &inDateTime, &theDateTimeSecs );
  93.  
  94.     outSeconds = longDateTimeLo( theDateTimeSecs );
  95. }
  96.  
  97.  
  98. // --------------------------------------------------
  99. //        • DateTimeToString
  100. // --------------------------------------------------
  101.  
  102. void
  103. UDateTime::DateTimeToString(
  104.     const LongDateRec &    inDateTime,
  105.     Str255            outDateString,
  106.     Str255            outTimeString )
  107. {
  108.     LongDateTime    theDateTimeSecs;
  109.     LongDateToSeconds( &inDateTime, &theDateTimeSecs );
  110.  
  111.     Handle    intlH;
  112.     Size        theSize;
  113.     intlH = ::GetIntlResource( k_intl_NumericFormat );    // as 'Intl0Rec'
  114.     theSize = ::GetHandleSize( intlH );
  115.  
  116.     StClearHandleBlock    theIntlH( theSize, true );
  117.     ::BlockMoveData( *intlH, *theIntlH, theSize );
  118.  
  119. //    ((Intl0Ptr)*theIntlH)->dateOrder = mdy;            // m/d/y order
  120. //    ((Intl0Ptr)*theIntlH)->shrtDateFmt = century;        // include century
  121. //    ((Intl0Ptr)*theIntlH)->shrtDateFmt |= mntLdingZ;    // lead 0 for month
  122. //    ((Intl0Ptr)*theIntlH)->shrtDateFmt |= dayLdingZ;    // lead 0 for day
  123. //    ((Intl0Ptr)*theIntlH)->shrtDateFmt &= ~century;    // suppress century
  124. //    ((Intl0Ptr)*theIntlH)->dateSep = '/';                // date separator
  125.  
  126.     ::LongDateString( &theDateTimeSecs, shortDate, outDateString, theIntlH );
  127.  
  128.     ((Intl0Ptr)*theIntlH)->timeCycle = timeCycle24;    // or; timeCycleZero, timeCycle12
  129.     ((Intl0Ptr)*theIntlH)->timeFmt |= secLeadingZ | minLeadingZ | hrLeadingZ;
  130. //    ((Intl0Ptr)*theIntlH)->timeSep = ':';                // time separator
  131.  
  132.     ::LongTimeString( &theDateTimeSecs, kWantSeconds, outTimeString, theIntlH );
  133. }
  134.  
  135.  
  136. // --------------------------------------------------
  137. //        • StringToDateTime
  138. // --------------------------------------------------
  139.  
  140. void
  141. UDateTime::StringToDateTime(
  142.     const Str255    inDateString,
  143.     const Str255    inTimeString,
  144.     LongDateRec &    outDateTime )
  145. {
  146.     Size                lengthUsed;
  147.     DateCacheRecord        theCache;
  148.     StringToDateStatus    stdStatus;
  149.  
  150.     ::InitDateCache( &theCache );
  151.  
  152.     stdStatus = ::StringToDate( (Ptr) &inDateString[1], inDateString[0],
  153.                 &theCache, &lengthUsed, &outDateTime );
  154.     stdStatus = ::StringToTime( (Ptr) &inTimeString[1], inTimeString[0],
  155.                 &theCache, &lengthUsed, &outDateTime );
  156. }
  157.  
  158.  
  159. // end of program
  160.